package com.devicemantra.coordinate.dataobjects;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOUserException;
import javax.jdo.ObjectState;
import javax.jdo.PersistenceManager;

import com.devicemantra.coordinate.ds.tests.PMF;
import com.devicemantra.coordinate.exceptions.dataobjects.CDatabaseException;
import com.devicemantra.coordinate.exceptions.operations.CException;
import com.devicemantra.coordinate.exceptions.operations.CUserAlreadyExistsException;
import com.devicemantra.coordinate.exceptions.operations.CUserDoesNotExistException;
import com.devicemantra.coordinate.jaxb.LoginInfo;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;

public class CUserFactory 
{
	
		public static boolean checkUserExists(String pUserName)
		{
			boolean lDoesExist = true;
			Key lNewUserKey = KeyFactory.createKey(CLoginInfo.class.getSimpleName(), pUserName);
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try
			{
				pm.getObjectById(CLoginInfo.class,lNewUserKey);
			} catch(JDOObjectNotFoundException e)
			{
				lDoesExist = false;
			}
			pm.close();
			return lDoesExist;
		}
		
		private static Key createKey(String pUserName)
		{
			Key lNewUserKey = KeyFactory.createKey(CLoginInfo.class.getSimpleName(), pUserName);
			return lNewUserKey;
		}
		
		private static CLoginInfo getUserByKey(String pUserKey)
		{
			CLoginInfo lLoginInfo = null;
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try
			{
				lLoginInfo = pm.getObjectById(CLoginInfo.class,pUserKey);
					pm.retrieve(lLoginInfo);
					lLoginInfo = pm.detachCopy(lLoginInfo);
					
			} catch(JDOObjectNotFoundException e)
			{
					System.out.println("[COORDINATE] ERROR: Some problem retreiving object");
					e.printStackTrace();
			}
			pm.close();
			return lLoginInfo;
		}
		
		public static CLoginInfo getUserLoginInfo(String pUserName)
		{
			CLoginInfo lLoginInfo = null;
			PersistenceManager pm = PMF.get().getPersistenceManager();
			if( checkUserExists(pUserName))
			{
				Key lUserKey = createKey(pUserName);
				try
				{
					lLoginInfo = pm.getObjectById(CLoginInfo.class,lUserKey);
					pm.retrieve(lLoginInfo);
					lLoginInfo = pm.detachCopy(lLoginInfo);
					
				} catch(JDOObjectNotFoundException e)
				{
					System.out.println("[COORDINATE] ERROR: Some problem retreiving object");
					e.printStackTrace();
				}
			}
			pm.close();
			return lLoginInfo;
		}
		
		
		public static boolean checkPasswordForUsername(String pUsername,
														String pPassword)
		{
			boolean lIsVerificationSuccess = false; // in a pessimistic mood 3rd Dec, 2009
			CLoginInfo lInfo = getUserLoginInfo(pUsername);
			if( lInfo.getmPassword().equals(pPassword))
			{
				lIsVerificationSuccess = true;
			}
			return lIsVerificationSuccess;
		}
		
		
		/* TODO Add @Deprecated here */
		public static CLoginInfo createNewLoginInfo(String pNewUserName,String pPassword)
		{
			/* First check whether such a Department exists using Keys */
			boolean lDoesUserExist = checkUserExists(pNewUserName);
			if(false == lDoesUserExist)
			{
				CLoginInfo lLoginInfo = new CLoginInfo(pNewUserName,pPassword);
				Key lNewDepartmentKey = createKey(pNewUserName);
				System.out.println("User Key is " + KeyFactory.keyToString((lNewDepartmentKey)));
				lLoginInfo.setKey(lNewDepartmentKey);
				return lLoginInfo;
			}
			else
			{
				/* User with that name already exists */
				return null;
			}
		}
		
		public static void createNewUser(String pNewUserName,String pPassword) throws CUserAlreadyExistsException, CDatabaseException
		{
			/* First check whether such a Department exists using Keys */
			boolean lDoesUserExist = checkUserExists(pNewUserName);
			if( true == lDoesUserExist )
			{
				throw new CUserAlreadyExistsException("ERROR: User already exists");
			}
			
			CLoginInfo lLoginInfo = new CLoginInfo(pNewUserName,pPassword);
			Key lNewDepartmentKey = createKey(pNewUserName);
			System.out.println("User Key is " + KeyFactory.keyToString((lNewDepartmentKey)));
			lLoginInfo.setKey(lNewDepartmentKey);
			
			try {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.makePersistent(lLoginInfo);
			pm.close();
			} catch(Exception e) {
				throw new CDatabaseException("ERROR: Could not persist UserLoginInfo");
			}
			
		}
		
		public static void modifyUser(String pUserName,String pNewPassword) throws CUserDoesNotExistException
		{
			Key lUserKey = createKey(pUserName);
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try
			{
				CLoginInfo lLoginInfo = pm.getObjectById(CLoginInfo.class,lUserKey);
				//pm.retrieve(lLoginInfo);
				lLoginInfo.setmPassword(pNewPassword);
				pm.makePersistent(lLoginInfo);
				pm.close();
				
			} catch(JDOObjectNotFoundException e)
			{
				System.out.println("[COORDINATE] ERROR: Some problem retreiving object");
				e.printStackTrace();
				throw new CUserDoesNotExistException("Some problem retreiving CLoginInfo object");
			}
			
		}
		
		
		
		
		public static void deleteUser(String pUserName) throws CUserDoesNotExistException, CDatabaseException
		{
			boolean lDoesExist = checkUserExists(pUserName);
			if(lDoesExist == false)
			{
				throw new CUserDoesNotExistException("ERROR: User info does not exist");
			}
			
			CLoginInfo lLoginInfo = null;
			Key lUserKey = createKey(pUserName);
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try
			{
				lLoginInfo = pm.getObjectById(CLoginInfo.class,lUserKey);
				pm.deletePersistent(lLoginInfo);
			} catch(JDOObjectNotFoundException e)
			{
				System.out.println("[COORDINATE] ERROR: Some problem retreiving object");
				e.printStackTrace();
				throw new CDatabaseException("ERROR: Object does not exist");
			}finally {
				if( pm != null )
					pm.close();
			}
			
		}
		
		

		
}
